package org.tloss.web.editor.database.editors;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.logging.Logger;

import org.tloss.web.editor.Utils;

public class DelegatingDriver implements Driver {
	private final Driver driver;

	public DelegatingDriver(Driver driver) {
		if (driver == null) {
			Utils.logError("Driver must not be null.", null);
			throw new IllegalArgumentException("Driver must not be null.");
		}
		this.driver = driver;
	}

	public Connection connect(String url, Properties info) throws SQLException {
		return driver.connect(url, info);
	}

	public boolean acceptsURL(String url) throws SQLException {
		return driver.acceptsURL(url);
	}

	public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
			throws SQLException {
		return driver.getPropertyInfo(url, info);
	}

	public int getMajorVersion() {
		return driver.getMajorVersion();
	}

	public int getMinorVersion() {
		return driver.getMinorVersion();
	}

	public boolean jdbcCompliant() {
		return driver.jdbcCompliant();
	}

	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		Class<?> c = driver.getClass();
		Method method;
		try {
			method = c.getDeclaredMethod("getParentLogger");
		} catch (SecurityException e) {
			throw new SQLFeatureNotSupportedException(e);
		} catch (NoSuchMethodException e) {
			throw new SQLFeatureNotSupportedException(e);
		}
		try {
			return (Logger) method.invoke(driver);
		} catch (IllegalArgumentException e) {
			throw new SQLFeatureNotSupportedException(e);
		} catch (IllegalAccessException e) {
			throw new SQLFeatureNotSupportedException(e);
		} catch (InvocationTargetException e) {
			throw new SQLFeatureNotSupportedException(e);
		}
	}

}
